//
//  NovelCell.m
//  Project
//
//  Created by liting on 2025/8/29.
//

#import "NovelCell.h"
#import "ItemModel.h"
#import <SDWebImage/SDWebImage.h>
#import "Masonry.h"

@interface  NovelCell()
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *priceLabel;

@end


@implementation NovelCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 8;
    self.contentView.layer.masksToBounds = YES;
    self.contentView.layer.borderWidth = 0.5;
    self.contentView.layer.borderColor = [UIColor colorWithWhite:0.9 alpha:1.0].CGColor;

    // 图片视图
    self.imageView = [[UIImageView alloc] init];
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    self.imageView.clipsToBounds = YES;
    self.imageView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
    [self.contentView addSubview:self.imageView];

    // 标题标签
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.font = [UIFont systemFontOfSize:14];
    self.titleLabel.textColor = [UIColor darkGrayColor];
    self.titleLabel.numberOfLines = 2;
    [self.contentView addSubview:self.titleLabel];

    // 价格标签
    self.priceLabel = [[UILabel alloc] init];
    self.priceLabel.font = [UIFont boldSystemFontOfSize:16];
    self.priceLabel.textColor = [UIColor redColor];
    [self.contentView addSubview:self.priceLabel];

    [self setupConstraints];
}

- (void)setupConstraints {
    self.imageView.translatesAutoresizingMaskIntoConstraints = NO;
    self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    self.priceLabel.translatesAutoresizingMaskIntoConstraints = NO;


    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView);
        make.leading.equalTo(self.contentView);
        make.trailing.equalTo(self.contentView);
        make.height.equalTo(self.imageView.mas_width);
    }];

    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imageView.mas_bottom).offset(6);
        make.leading.equalTo(self.contentView).offset(6);
        make.trailing.equalTo(self.contentView).offset(6);
        make.height.mas_equalTo(20);
    }];


    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(6);
        make.leading.equalTo(self.contentView).offset(6);
        make.trailing.equalTo(self.contentView).offset(6);
        make.height.mas_equalTo(20);
    }];

}

- (void)configureWithModel:(ItemModel *)model {
    self.titleLabel.text = model.title;
    self.priceLabel.text = model.title;

    // 加载图片
    if (model.image) {
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:model.image]
                          placeholderImage:[UIImage imageNamed:@"placeholder"]];
    } else {
        self.imageView.image = [UIImage imageNamed:@"placeholder"];
    }
}

@end
